﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using ProjTemp.Desktop.Common.ViewModel;
using ProjTemp.Common.Logging;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.ServiceLocation;
using ProjTemp.Desktop.Common.Events;
using Mysoft.ServerMonitoring.OMKit.Infrastructure.Events;

namespace Mysoft.ServerMonitoring.OMKit.ClientServers.ViewModels
{
    [Export(typeof(ClientServersViewModel))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    internal class ClientServersViewModel : VMBase<List<ClientGroup>>
    {
        [ImportingConstructor]
        public ClientServersViewModel(ILogger logger, IRegionManager regionManager, IEventAggregator eventAggregator, IServiceLocator serviceLocator)
            : base(logger, regionManager, eventAggregator, serviceLocator)
        {
            this._logger.LogType = typeof(ClientServersViewModel);

            this.ConstructModel();
        }

        public void RequestFreshData(object data)
        {
            ClientServer payload = null;
            if (data is ClientInfo)
            {
                var clientInfo = data as ClientInfo;
                payload = new ClientServer() { ClientId = clientInfo.Id, ServerId = Guid.Empty, ClientName = clientInfo.ClientName, ServerName = string.Empty };
            }
            else if (data is ServerInfo)
            {
                var serverInfo = data as ServerInfo;
                payload = new ClientServer() { ClientId = serverInfo.ClientId, ServerId = serverInfo.Id, ClientName= serverInfo.ClientName, ServerName = serverInfo.ServerName};
            }
            if (payload != null)
                this._eventAggregator.GetEvent<ClientServerSelected>().Publish(payload);
        }

        private void ConstructModel()
        {
            var clientGroups = new List<ClientGroup>();

            var clientGroup = new ClientGroup() { GroupName = "上海" };

            var clientInfo = new ClientInfo() { ClientName = "上海新城" };
            var serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientInfo = new ClientInfo() { ClientName = "上海SOHO" };
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientGroups.Add(clientGroup);

            clientGroup = new ClientGroup() { GroupName = "广州" };

            clientInfo = new ClientInfo() { ClientName = "华润置地" };
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientInfo = new ClientInfo() { ClientName = "中山美的" };
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientGroups.Add(clientGroup);

            clientGroup = new ClientGroup() { GroupName = "重庆" };

            clientInfo = new ClientInfo() { ClientName = "重庆龙湖" };
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientInfo = new ClientInfo() { ClientName = "福建融侨" };
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientInfo = new ClientInfo() { ClientName = "万科集团" };
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.1", ServerName = "DB Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.2", ServerName = "Web Server" };
            clientInfo.Servers.Add(serverInfo);
            serverInfo = new ServerInfo() { ClientId = clientInfo.Id, ClientName = clientInfo.ClientName, IPAddress = "192.168.1.3", ServerName = "Report Server" };
            clientInfo.Servers.Add(serverInfo);
            clientGroup.Clients.Add(clientInfo);

            clientGroups.Add(clientGroup);

            this.Model = clientGroups;
        }
    }

    internal sealed class ClientGroup
    {
        public Guid Id { get; set; }
        public string GroupName { get; set; }
        public List<ClientInfo> Clients { get; set; }

        public ClientGroup()
        {
            this.Id = Guid.NewGuid();
            this.Clients = new List<ClientInfo>();
        }
    }

    internal sealed class ClientInfo
    {
        public Guid Id { get; set; }
        public string ClientName { get; set; }
        public List<ServerInfo> Servers { get; set; }

        public ClientInfo()
        {
            this.Id = Guid.NewGuid();
            this.Servers = new List<ServerInfo>();
        }
    }

    internal sealed class ServerInfo
    {
        public Guid Id { get; set; }
        public Guid ClientId { get; set; }
        public string IPAddress { get; set; }
        public string ServerName { get; set; }
        public string ClientName { get; set; }

        public ServerInfo()
        {
            this.Id = Guid.NewGuid();
        }

        public override string ToString()
        {
            return string.Format("{0}({1})", this.IPAddress, this.ServerName);
        }
    }
}
